﻿using System;
using System.Windows.Forms;
using DataTransfer;
using EnglishLibrary;
using EnglishReminder.Properties;
using Global;

namespace EnglishReminder
{
    public partial class CreateCategory : Form
    {
        CategoryDT cateDT = new CategoryDT();
        Category cate = new Category();

        public CreateCategory()
        {
            InitializeComponent();
        }

        #region Events handlers
        private void btnCreate_Click(object sender, EventArgs e)
        {
            var setCateResult = SetCateValue();
            if (!setCateResult)
                return;

            var result = cateDT.InsertItem(cate);
            if (result == false)
            {
                MessageBox.Show(Resources.Result_CannotCreateCate, "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {   
                MessageBox.Show(Resources.Result_CreateCateSuccessfully, "Successful", MessageBoxButtons.OK);
                UpdateCateToSettings();
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region Private methods
        private bool SetCateValue()
        {
            var name = this.tbName.Text.Trim();
            var desciption = this.rtbDescription.Text.Trim();

            if (name == string.Empty)
            {
                MessageBox.Show(Resources.Warning_CategoryNameEmpty);
                return false;
            }

            //Check cate name (unique in database)
            if (cateDT.GetCateByName(name) != null)
            {
                MessageBox.Show(Resources.Warning_CateAlreadyExisted, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            cate.Name = name;
            cate.Description = desciption;
            cate.CreatedDate = DateTime.Today;
            return true;
        }
        private void UpdateCateToSettings()
        {
            Global.Settings.CATEGORY_LIST_NO_DISPLAY.Add(cate.CategoryID, cate.Name);
        }
        #endregion
    }
}
